package com.wing.sell.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.web.annotation.RequestObjectFieldIgnore;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.sell.model.entity.CustomerDueSubjectCompany;
import com.wing.sell.model.request.CustomerDueSubjectCompanyForm;
import com.wing.sell.service.CustomerDueSubjectCompanyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

/**
 * @author
 */
@Validated
@Api(value = "Admin-客户尽调科目公司管理", tags = "#/sell/customerDueSubjectCompany")
@RestController("customerDueSubjectCompanyController")
@RequestMapping("sell/admin/customerDueSubjectCompany")
@RequiredArgsConstructor
public class CustomerDueSubjectCompanyController {

    private final CustomerDueSubjectCompanyService customerDueSubjectCompanyService;


    @ApiOperation(value = "查询所有客户尽调科目公司", notes = "搜索")
    @GetMapping("/list")
    public PageResult<CustomerDueSubjectCompany> list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit,
            @RequestParam(defaultValue = "create_time") String sort,
            @RequestParam(defaultValue = "asc") String order,
            Long subjectId) {
        Page<CustomerDueSubjectCompany> pg = new Page<>(page, limit);
        QueryWrapper<CustomerDueSubjectCompany> queryWrapper = new QueryWrapper<>();
        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            if (StringUtils.equals("asc", order)) {
                queryWrapper.orderByAsc(StrUtil.toUnderlineCase(sort));
            } else {
                queryWrapper.orderByDesc(StrUtil.toUnderlineCase(sort));
            }
        }
        if (subjectId != null) {
            queryWrapper.eq("subject_id", subjectId);
        }
        IPage<CustomerDueSubjectCompany> list = customerDueSubjectCompanyService.getBaseMapper().selectPage(pg, queryWrapper);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }


    @ApiOperation(value = "添加客户尽调科目公司", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/add")
    public JsonResult add(@Validated(CustomerDueSubjectCompanyForm.Add.class) CustomerDueSubjectCompanyForm customerDueSubjectCompanyForm) {
        if (customerDueSubjectCompanyService.add(customerDueSubjectCompanyForm) > 0) {
            return JsonResult.success("添加成功");
        }
        return JsonResult.fail("添加失败");
    }


    @ApiOperation(value = "修改客户尽调科目公司", notes = "修改")
    @PutMapping("/update")
    public JsonResult update(@Validated(CustomerDueSubjectCompanyForm.Update.class) CustomerDueSubjectCompanyForm customerDueSubjectCompanyForm) {
        CustomerDueSubjectCompany customerDueSubjectCompany = customerDueSubjectCompanyService.getById(customerDueSubjectCompanyForm.getId());
        if (customerDueSubjectCompany == null) {
            return JsonResult.fail("修改失败！");
        }
        CopyOptions copyOptions = CopyOptions.create()
                .setEditable(CustomerDueSubjectCompany.class)
                .setIgnoreError(true)
                .setIgnoreNullValue(true);
        BeanUtil.copyProperties(customerDueSubjectCompanyForm, customerDueSubjectCompany, copyOptions);
        if (customerDueSubjectCompanyService.updateById(customerDueSubjectCompany)) {
            return JsonResult.success("修改成功！");
        }
        return JsonResult.fail("修改失败！");
    }


    @ApiOperation(value = "删除客户尽调科目公司", notes = "删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "customerDueSubjectCompanyId", value = "客户尽调科目公司id", required = true, dataType = "Integer", paramType = "path")
    })
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long customerDueSubjectCompanyId) {
        if (customerDueSubjectCompanyService.removeById(customerDueSubjectCompanyId)) {
            return JsonResult.success("删除成功");
        }
        return JsonResult.fail("删除失败");
    }

    /**
     * 获取岗位角色数据
     *
     * @param subjectId
     * @return
     */
    @GetMapping("/getRoleData")
    public JsonResult getRoleData(Long subjectId) {
        return customerDueSubjectCompanyService.getRoleData(subjectId);
    }

}
